package com.cro.prison.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cro.prison.dao.DailyBehaviorDao;
import com.cro.prison.dao.PrisonDao;
import com.cro.prison.entity.constant.BEHAVIORTYPEENUM_PRISONER;
import com.cro.prison.entity.pojo.DailyBehavior;
import com.cro.prison.entity.pojo.Prisoner;
import com.cro.prison.entity.vo.AddPrisonerVo;
import com.cro.prison.service.PrisonService;
import com.cro.prison.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PrisonServiceImpl extends ServiceImpl<PrisonDao, Prisoner> implements PrisonService {

    @Autowired
    DailyBehaviorDao dailyBehaviorDao;

    @Override
    public List<Prisoner> listByDate() {

        Calendar cal = Calendar.getInstance();
        String mouth = DateUtils.customFormat(DateUtils.addDay(cal.getTime(),-1));
        QueryWrapper<Prisoner> queryWrapper = new QueryWrapper<Prisoner>().ge("create_time", mouth);
        List<Prisoner> prisoners = this.baseMapper.selectList(queryWrapper);
        return prisoners;
    }

    @Override
    public String analysisAndPrediction(Long id) {
        Calendar calendar = Calendar.getInstance();
        Date date = DateUtils.addDay(calendar.getTime(),-7);
//        System.out.println(df.format(dateBefore7Days));
        String dateStr = DateUtils.customFormat(date);
        List<DailyBehavior> dailyBehaviors = dailyBehaviorDao.selectList(new QueryWrapper<DailyBehavior>().eq("person_id", id).lambda().ge(DailyBehavior::getCreateTime,dateStr));
        int sleepLate = 0;
        int laughAloud = 0;
        int cry = 0;

        for (DailyBehavior dailyBehavior : dailyBehaviors) {
            if(dailyBehavior.getAbnormalBehaviorType() == BEHAVIORTYPEENUM_PRISONER.SLEEP_LATE.getCode()){
                sleepLate++;
            }else if(dailyBehavior.getAbnormalBehaviorType() == BEHAVIORTYPEENUM_PRISONER.LAUGH_ALOUD.getCode()){
                laughAloud++;
            }else if(dailyBehavior.getAbnormalBehaviorType() == BEHAVIORTYPEENUM_PRISONER.CRY.getCode()){
                cry++;
            }
        }

        Prisoner prisoner = this.baseMapper.selectById(id);
        String s0 = "罪犯" + prisoner.getName() + "：" + "近期";
        String s1 = "睡眠正常，";
        String s2 = "大笑次数正常，";
        String s3 = "情绪稳定";
        if(sleepLate > 3){
            s1 = "经常晚睡，";
        }
        if(laughAloud > 10){
            s2 = "大笑次数超常，";
        }
        if(cry > 5 && cry <= 10){
            s3 = "偶尔哭泣，情绪较不稳定";
        }else if(cry >= 10){
            s3 = "经常哭泣，情绪非常不稳定";
        }

        String analyseInfo = s0 + s1 + s2 + s3;

        return analyseInfo;
    }



    @Override
    public List<Integer> selectByType(Integer prisonerType) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",prisonerType);
        List<Prisoner> prisonList = this.baseMapper.selectList(queryWrapper);
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        for (Prisoner prisoner : prisonList) {
            switch (prisoner.getMood()){
                case 1: a++;
                    break;
                case 2: b++;
                    break;
                case 3: c++;
                    break;
                case 4: d++;
                    break;
                case 5: e++;
                    break;
            }

        }

        List<Integer> list = Arrays.asList(a,b,c,d,e);
        return list;
    }

    @Override
    public void addPrisoner(AddPrisonerVo prisoner) throws ParseException {



        Prisoner newPrisoner = new Prisoner();
        BeanUtils.copyProperties(prisoner, newPrisoner);

        Date date1 = prisoner.getDate1();
        Date date2 = prisoner.getDate2();
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("YYYY-MM-dd");
        Date birthDay = simpleDateFormat1.parse(prisoner.getBirth());
        Integer age = getAge(birthDay);
        newPrisoner.setAge(age);

        String date1Format = simpleDateFormat1.format(date1);
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("HH:mm:ss");
        String date2format = simpleDateFormat2.format(date2);

        String date = date1Format +" "+ date2format;
        SimpleDateFormat simpleDateFormatD = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date createTime = simpleDateFormatD.parse(date);
        new java.sql.Date(createTime.getTime());
        newPrisoner.setCreateTime(createTime);

//        SimpleDateFormat simpleDateFormatB = new SimpleDateFormat("yyyy/MM/dd");
//        String resultBirth = simpleDateFormatB.format(birth);
//        newPrisoner.setBirth(resultBirth);

        this.baseMapper.insert(newPrisoner);

    }

    @Override
    public Prisoner getPrisonerByIdNumber(String idNumber) {
        QueryWrapper<Prisoner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_number", idNumber);
        Prisoner prisoner = this.baseMapper.selectOne(queryWrapper);
        return prisoner;
    }


    /**
     * 根据出生年月计算年龄
     * @param birth
     * @return java.lang.Integer
     **/
    private Integer getAge(Date birth) {
        
        Calendar cal = Calendar.getInstance();

        //当前的年月日
        int yearNow = cal.get(Calendar.YEAR);  //年
        int monthNow = cal.get(Calendar.MONTH);  //月
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //日

        cal.setTime(birth);//出生时间Date
        //出生的年月日
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth)
                    age--;
            }else{
                age--;
            }

        }
//        String result = String.valueOf(age);//int转为String类型
        return age;

    }


}
